﻿using System;
using UnityEngine;
using UnityEngine.Events;
using UnityEngine.UI;
using Yoozoo.Managers.ResourceManagerV2.Runtime;
using Yoozoo.UI;

public class ImageBinder : BinderBase
{
    public enum AttributeType : int
    {
        spriteName,
        sprite,
        overrideSprite,
        preserveAspect,
        fillCenter,
        fillAmount,
        fillClockwise,
        fillOrigin,
        alphaHitTestMinimumThreshold,
        useSpriteMesh,
        pixelsPerUnitMultiplier,
        onCullStateChanged,
        maskable,
        isMaskingGraphic,
        color,
        raycastTarget,
        enabled,
    }

    private Image _target;
    private ResLoader _resLoader;

    public ImageBinder(UIExpansion owner, LinkerConfig config) : base(owner, config)
    {
        _resLoader = ResLoader.Alloc();
    }

    public ImageBinder()
    {
    }

    public override void Dispose()
    {
        base.Dispose();
        
        _resLoader.Recycle2Cache();
        _resLoader = null;
        _target = null;
        _active = default;
    }

    public override void Init(LinkerConfig config)
    {
        GameObject targetGo = _owner.GetStoredGameObject(config.StoredGameObjectIndex);
        if (targetGo == null)
        {
            return;
        }
        _resLoader = ResLoader.Alloc();
        _target = targetGo.GetComponent<Image>();
        _active = _target != null;
    }
    private static string prefixUrl = "Assets/ResourcesAssets/{0}.png";
    public override void SetString(string value)
    {
        switch ((AttributeType)Enum.Parse(typeof(AttributeType), _linkerType))
        {
            case AttributeType.spriteName:
                if (string.IsNullOrEmpty(value))
                {
                    _target.sprite = null;
                    _target.gameObject.SetActive(false);
                    return;
                }
                //var lastIndexOf = value.LastIndexOf("/");
                //string bundleName = value.Substring(0, lastIndexOf);
                //string spritename = value.Substring(lastIndexOf + 1);
                //string atlasName = "Assets/ResourcesAssets/" + bundleName + ".spriteatlas";

                //if (!UMTResource.isEditorMode && UMTResource.HasAsset(atlasName) != HasAssetResult.NotExist)
                //{
                //    _resLoader.Add2Load(atlasName,
                //        (success, name, asset) =>
                //        {
                //            if (!success)
                //            {
                //                UnityEngine.Debug.LogError("dont has this spriteatlas:" + bundleName);
                //            }
                //            else
                //            {
                //                var spriteatlas = asset as SpriteAtlas;
                //                _target.sprite = spriteatlas.GetSprite(spritename);
                //                _target.gameObject.SetActive(true);
                //            }
                //        }).Load();
                //}
                //else
                {
                    _resLoader.Add2LoadRapid(string.Format(prefixUrl,value), typeof(Sprite),
                        (success, name, asset) =>
                        {
                            if (!success)
                            {
                                UnityEngine.Debug.LogError("dont has this sprite:" + value);
                            }
                            else
                            {
                                _target.sprite = asset as Sprite;
                                _target.gameObject.SetActive(true);
                            }
                        }).Load();
                }
                break;
            default:
                break;
        }

        base.SetString(value);
    }

    public override void SetSprite(Sprite value)
    {
        switch ((AttributeType)Enum.Parse(typeof(AttributeType), _linkerType))
        {
            case AttributeType.sprite:
                _target.sprite = value;
                break;
            case AttributeType.overrideSprite:
                _target.overrideSprite = value;
                break;
            default:
                break;
        }

        base.SetSprite(value);
    }

    public override void SetBoolean(bool value)
    {
        switch ((AttributeType)Enum.Parse(typeof(AttributeType), _linkerType))
        {
            case AttributeType.preserveAspect:
                _target.preserveAspect = value;
                break;
            case AttributeType.fillCenter:
                _target.fillCenter = value;
                break;
            case AttributeType.fillClockwise:
                _target.fillClockwise = value;
                break;
            case AttributeType.useSpriteMesh:
                _target.useSpriteMesh = value;
                break;
            case AttributeType.maskable:
                _target.maskable = value;
                break;
            case AttributeType.isMaskingGraphic:
                //_target.isMaskingGraphic = value;
                break;
            case AttributeType.raycastTarget:
                _target.raycastTarget = value;
                break;
            case AttributeType.enabled:
                _target.enabled = value;
                break;
            default:
                break;
        }

        base.SetBoolean(value);
    }

    public override void SetSingle(float value)
    {
        switch ((AttributeType)Enum.Parse(typeof(AttributeType), _linkerType))
        {
            case AttributeType.fillAmount:
                _target.fillAmount = value;
                break;
            case AttributeType.alphaHitTestMinimumThreshold:
                _target.alphaHitTestMinimumThreshold = value;
                break;
            case AttributeType.pixelsPerUnitMultiplier:
                //_target.pixelsPerUnitMultiplier = value;
                break;
            default:
                break;
        }

        base.SetSingle(value);
    }

    public override void SetInt32(int value)
    {
        switch ((AttributeType)Enum.Parse(typeof(AttributeType), _linkerType))
        {
            case AttributeType.fillOrigin:
                _target.fillOrigin = value;
                break;
            default:
                break;
        }

        base.SetInt32(value);
    }

    public override void SetActionBoolean(UnityAction<bool> action)
    {
        switch ((AttributeType)Enum.Parse(typeof(AttributeType), _linkerType))
        {
            case AttributeType.onCullStateChanged:
                _target.onCullStateChanged.AddListener(action);
                break;
            default:
                break;
        }

        base.SetActionBoolean(action);
    }

    public override void RemoveActionBoolean(UnityAction<bool> action)
    {
        switch ((AttributeType)Enum.Parse(typeof(AttributeType), _linkerType))
        {
            case AttributeType.onCullStateChanged:
                _target.onCullStateChanged.RemoveListener(action);
                break;
            default:
                break;
        }

        base.RemoveActionBoolean(action);
    }

    public override void SetColor(Color value)
    {
        switch ((AttributeType)Enum.Parse(typeof(AttributeType), _linkerType))
        {
            case AttributeType.color:
                _target.color = value;
                break;
            default:
                break;
        }

        base.SetColor(value);
    }

    public override void RemoveAllAction()
    {
        switch ((AttributeType)Enum.Parse(typeof(AttributeType), _linkerType))
        {
            case AttributeType.onCullStateChanged:
                _target.onCullStateChanged.RemoveAllListeners();
                break;
            default:
                break;
        }
    }

}
